package cn.hpu.mobile_bank.service.impl;

import cn.hpu.mobile_bank.domain.Mobile;
import cn.hpu.mobile_bank.domain.MobileAccount;
import cn.hpu.mobile_bank.dto.resp.AccountSelectResp;
import cn.hpu.mobile_bank.dto.resp.ApiResult;
import cn.hpu.mobile_bank.exception.BusinessException;
import cn.hpu.mobile_bank.exception.CommonErrorEnum;
import cn.hpu.mobile_bank.mapper.MobileAccountMapper;
import cn.hpu.mobile_bank.mapper.MobileMapper;
import cn.hpu.mobile_bank.utils.RequestHolder;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hpu.mobile_bank.domain.Account;
import cn.hpu.mobile_bank.service.AccountService;
import cn.hpu.mobile_bank.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.schema.CollectionType;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author DELL
* @description 针对表【account】的数据库操作Service实现
* @createDate 2024-06-12 17:30:07
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
    implements AccountService{

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private MobileMapper mobileMapper;

    @Autowired
    private MobileAccountMapper mobileAccountMapper;


    /**
     * 根据电话号码获取账户列表。
     * <p>
     * 本方法通过调用accountMapper的selectByTelephone方法，根据电话号码查询数据库中的账户信息。
     * 如果电话号码不存在，则抛出BusinessException异常，指示登录错误。
     * 方法返回ApiResult对象，其中包含查询到的账户列表。如果查询成功，ApiResult的status为200，data字段为账户列表。
     *
     * @return ApiResult<List<Account>> 包含查询到的账户列表的ApiResult对象。
     * @throws BusinessException 如果电话号码为空，则抛出业务异常，指示登录错误。
     */
    @Override
    public ApiResult<List<Account>> accountList() {
        // 从请求持有者中获取电话号码
        String telephone = RequestHolder.get().getTelephone();
        // 检查电话号码是否为空，如果为空则抛出登录错误异常
        if(telephone == null) throw new BusinessException(CommonErrorEnum.LOGIN_ERROR);
        // 根据电话号码查询账户列表
        List<Account> accounts = accountMapper.selectByTelephone(telephone);
        // 返回查询结果，包装在ApiResult中
        return ApiResult.success(accounts);
    }

    @Override
    public ApiResult<List<Account>> accountListNot() {
        String telephone = RequestHolder.get().getTelephone();
        if(telephone == null) throw new BusinessException(CommonErrorEnum.LOGIN_ERROR);
        List<Account> accounts = accountMapper.selectByTelephoneNot(telephone);

        return ApiResult.success(accounts);
    }

    @Override
    public ApiResult<String> association(Integer accountId) {
        String telephone = RequestHolder.get().getTelephone();
        Account accounts = accountMapper.selectByTelephoneAndAccountId(telephone, accountId);
        //查询是否已经绑定过了
        if(accounts != null)
            throw new BusinessException("此账户已经绑定过了");

        //绑定信息
        Mobile mobile = mobileMapper.selectByTelephone(telephone);
        if(mobile == null) return ApiResult.fail(CommonErrorEnum.PARAM_INVALID);

        MobileAccount mobileAccount = new MobileAccount();
        mobileAccount.setAccountId(accountId);
        mobileAccount.setTelId(mobile.getId());
        mobileAccountMapper.insert(mobileAccount);

        return ApiResult.success();
    }

    @Override
    public ApiResult<List<AccountSelectResp>> accountNameList() {
        String telephone = RequestHolder.get().getTelephone();
        if(telephone == null) throw new BusinessException(CommonErrorEnum.LOGIN_ERROR);
        List<Account> accounts = accountMapper.selectByTelephone(telephone);
        List<AccountSelectResp> result = accounts.stream().map(account -> {
            AccountSelectResp accountSelectResp = new AccountSelectResp();
            accountSelectResp.setValue(account.getAccountName());
            accountSelectResp.setLabel(account.getAccountName());
            return accountSelectResp;
        }).collect(Collectors.toList());
        return ApiResult.success(result);
    }
}




